package main

import (
	"fmt"
	"reflect"
)

func MethodCallByID(obj_id uint64, method_name string, params ...interface{}) ([]interface{}, bool) {
	fmt.Println("Calll Method ...")
	if val, found := Map_Objects[obj_id]; found {
		fmt.Println(obj_id, val, found, params)
		if len(params) == 0 {
			return MethodCall(val, method_name)
		} else {
			return MethodCall(val, method_name, params...)
		}
	} else {
		fmt.Println(obj_id, val, found)
		return nil, false
	}
}

func MethodCall(obj interface{}, method_name string, params ...interface{}) ([]interface{}, bool) {
	meth := reflect.ValueOf(obj).MethodByName(method_name)
	if meth.IsValid() == false {
		fmt.Println("Method is Invalid!", method_name)
		return nil, false
	}

	in := make([]reflect.Value, len(params))
	for k, arg := range params {
		in[k] = reflect.ValueOf(arg)
	}

	var res []reflect.Value

	res = meth.Call(in)

	out := make([]interface{}, len(res))
	for k, val := range res {
		out[k] = val.Interface()
	}

	return out, true
}
